package com.adams.utils;

import java.io.IOException;
import java.net.URL;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 
 * 
 * File Name : SpringBeanFactoryUtil.java
 * 
 * @Description : SpringBean 构造器
 * @author 903819
 */
public class SpringBeanFactoryUtil implements ApplicationContextAware,
		BeanFactoryPostProcessor
{

	private static Logger logger = Logger.getLogger(SpringBeanFactoryUtil.class
			.getName());

	private static ApplicationContext cxt = null;

	public static Object getBean(String id)
	{
		return cxt.getBean(id);
	}

	public static boolean containsBeanName(String id)
	{
		boolean result = false;
		result = cxt.containsBean(id);
		return result;
	}

	public static ApplicationContext getApplicationContext()
	{
		return cxt;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException
	{
		cxt = applicationContext;
	}

	/**
	 * Description : 动态增加spring配置文件进入spring容器上下文中
	 * 
	 * @param configLocationString
	 * 
	 */
	public static void addApplicationContextXml(String configLocationString,
			ConfigurableListableBeanFactory beanFactory)
	{
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
				(BeanDefinitionRegistry) beanFactory);
		beanDefinitionReader.setResourceLoader(getApplicationContext());
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(
				getApplicationContext()));
		try
		{
			String[] configLocations = new String[]
			{ configLocationString };
			for (int i = 0; i < configLocations.length; i++)
				beanDefinitionReader
						.loadBeanDefinitions(getApplicationContext()
								.getResources(configLocations[i]));
		} catch (BeansException e)
		{
			logger.error("动态加载spring配置文件" + configLocationString + "出错!", e);
		} catch (IOException e)
		{
			logger.error("动态加载spring配置文件" + configLocationString + "出错!", e);
		}
	}

	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException
	{
		Set<String> packs = ResourceUtil.ADAMS_BUSINESS_PACK;
		for (String pack : packs)
		{
			String businessName = pack.substring(ResourceUtil.ADAMS_PACK
					.length() + 1, pack.length());
			String businessxml = pack + ResourceUtil.PACK_FILE_SEP
					+ "applicationContext-" + businessName + ".xml";
			URL fileUrl = Thread.currentThread().getContextClassLoader()
					.getResource(businessxml);
			if (null != fileUrl)
			{
				addApplicationContextXml("classpath:" + businessxml,
						beanFactory);
			}
		}
	}

}
